import { Component } from '@angular/core';
import { IonicPage, NavController, NavParams } from 'ionic-angular';

import { NewsinfoPage } from '../newsinfo/newsinfo';
import { LookService } from '../../services/look-service';

@IonicPage()
@Component({
  selector: 'page-news-one',
  templateUrl: 'news-one.html',
})
export class NewsOnePage {
  public list = [];
  public listData = [];
  public testList;
  public errorMessage:string;
  public gaming;
  public gaming1;
  public gaming2;

  constructor(
    public navCtrl: NavController, 
    public navParams: NavParams,
    public lookservice: LookService
  ) {}

  ionViewDidLoad() {
    let that = this
    for (let i = 0; i < 15; i++) {
      this.list.push('这是第'+i+'条数据')
    }
    for(let y = 0; y < 15; y++){
      let obj = {}
      obj["index"] = y+1;
      obj["value"] = "option"+y;
      obj["text"] = y+"_OptionOptionOptionOptionOption";
      this.listData.push(obj)
    }

    // 请求数据
    this.lookservice.getAllcategory()
    // 方法一
    .subscribe(
      countries => that.testList = <any>countries,
      error => that.errorMessage =<any>error,
      function complete(){
          console.log(that.testList)
          console.log('方法一')
      }
    )
    
    // 方法二
    // .subscribe(
    //     (data) => {
    //       that.testList = data
    //       console.log(that.testList)
    //       console.log('方法二')
    //     },
    //     (error) => {
    //       console.log(error.message)
    //     }
    // );
    
    // 方法三
    // .toPromise().then(
    //     (res) => {
    //         if (res) {
    //           that.testList = res
    //           console.log(that.testList)
    //           console.log('方法三')
    //         } else {
    //             // 其他处理
    //         }
    //     },
    //     (error) => {
    //         // 其他处理
    //     }
    // );

    //方法四
    // .subscribe(
    //   function(data){
    //     that.testList = data;
    //     console.log(that.testList)
    //     console.log('方法四')
    //   },
    //   function(error){
    //     console.log(error.message)
    //   }
    // )
 
  }

  goInfo(e){ /*带参数*/
      this.navCtrl.push(NewsinfoPage,{
          name:"gray",     
          job:"IT",
          parent:e    
      });
  }

  getValue(){
    console.log(this.gaming,this.gaming1,this.gaming2)
  }

  doInfinite(infiniteScroll){
     console.log("Begin async operation")
     setTimeout(() =>{
       for(let i = 0; i < 15; i++){
           this.list.push(this.list.length)
       }
       console.log("Async opertion has ended")
       infiniteScroll.complete();
     },500)
  }

}
